#pragma once
#include <string.h>
#include <assert.h>
namespace lw
{
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;
		iterator begin()
		{
			return _str;
		}
		const_iterator begin() const
		{
			return _str;
		}
		iterator end()
		{
			return _str + _size;
		}
		const_iterator end() const
		{
			return _str + _size;
		}
		string(const char* str = "")
		{
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_capacity + 1];

			strcpy(_str, str);

			std::cout << "string(const char* str = \"\")" << std::endl;
		}
		string(const string& s)
		{
			string tmp(s.c_str());
			swap(tmp);
			std::cout << "string(const string& s)" << std::endl;

		}
		string(string&& s)
		{
			swap(s);
			std::cout << "string(string&& s)" << std::endl;

		}
		string& operator=(const string& s)
		{
			if (this != &s)
			{
				string tmp(s);
				swap(tmp);
			}
			return *this;
			std::cout << "string& operator=(const string& s)" << std::endl;
		}
		string& operator=(string&& s)
		{
			swap(s);
			return *this;
			std::cout << "string& operator=(string&& s)" << std::endl;
		}
		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = _capacity = 0;
		}
		void swap(string& s)
		{
			std::swap(_str, s._str);
			std::swap(_size, s._size);
			std::swap(_capacity, s._capacity);
		}
		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n + 1];
				strcpy(tmp, _str);
				if (_str)
				{
					delete[] _str;
				}
				_str = tmp;
				_capacity = n;
			}
		}
		void push_back(char ch)
		{
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : 2 * _capacity);
			}
			_str[_size++] = ch;
			_str[_size] = '\0';
		}
		void append(const char* str)
		{
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(len + _size);
			}
			strcpy(_str + _size, str);
			_size += len;
		}
		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}
		string& operator+=(const char* str)
		{
			append(str);
			return *this;
		}
		void insert(size_t pos, char ch)
		{
			assert(pos <= _size);
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : 2 * _capacity);
			}
			size_t end = _size + 1;
			while (end > pos)
			{
				_str[end] = _str[end - 1];
				end--;
			}
			_str[pos] = ch;
			_size++;
		}
		void insert(size_t pos, const char* str)
		{
			assert(pos <= _size);
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(len + _size);
			}
			int end = _size;
			while (end >= (int)pos)
			{
				_str[end + len] = _str[end];
				end--;
			}
			strncpy(_str + pos, str, len);
			_size += len;
		}
		void erase(size_t pos, size_t len = npos)
		{
			assert(pos < _size);

			len = len > _size - pos ? _size - pos : len;
			while (pos + len <= _size)
			{
				_str[pos] = _str[pos + len];
				pos++;
			}
			_size -= len;
		}
		void clear()
		{
			_str[0] = '\0';
			_size = 0;
		}
		char& operator[](size_t pos)
		{
			assert(pos < _size);

			return _str[pos];
		}
		const char& operator[](size_t pos) const
		{
			assert(pos < _size);

			return _str[pos];
		}
		const char* c_str() const
		{
			return _str;
		}
		size_t size() const
		{
			return _size;
		}
		size_t capacity() const
		{
			return _capacity;
		}
	private:
		char* _str = nullptr;
		size_t _size = 0;
		size_t _capacity = 0;

		const static size_t npos = -1;
	};

	std::ostream& operator<<(std::ostream& out, const string& s)
	{
		for (const char& ch : s)
		{
			out << ch;
		}
		return out;
	}
	std::istream& operator>>(std::istream& in, string& s)
	{
		char ch = in.get();
		while (ch != ' ' && ch != '\n')
		{
			s += ch;
			ch = in.get();
		}
		return in;
	}

	void testString1()
	{
		string s;
		string s1("hello world");

		std::cout << s.c_str() << std::endl;
		std::cout << s1.c_str() << std::endl;
	}

	void testString2()
	{
		string s;
		s += 'a';
		s += "hello world";
		std::cout << s.c_str() << std::endl;

	}

	void testString3()
	{
		string s;
		s.insert(0, 'b');
		s.insert(0, "#https://www.baidu.com/herf");
		std::cout << s.c_str() << std::endl;

		s.erase(10, 5);
		std::cout << s.c_str() << std::endl;

		s.erase(0, 3);
		std::cout << s.c_str() << std::endl;

		s.erase(2);
		std::cout << s.c_str() << std::endl;

		string s1 = s;
		s1 += "aaaaaaaaaa";

		s = s1;
		std::cout << s.c_str() << std::endl;
	}

}